<!doctype html>
<html lang="en">

<head>
	<meta charset="UTF-8" />
	<title>北京循一堂健康管理有限公司</title>
	<link rel="stylesheet" href="../css/page.css">
	<link href="../css/bootstrap.min.css" rel="stylesheet" type="text/css">
	<link href="../plugins/bootstrap-table/bootstrap-table.min.css" rel="stylesheet" type="text/css">
	<script src="../js/jquery.min.js"></script>
	<script src="../js/bootstrap.min.js"></script>
	<script src="../plugins/select/js/bootstrap-select.min.js"></script>

	<script src="../plugins/bootstrap-table/bootstrap-table.min.js"></script>
	<script src="../plugins/bootstrap-table/bootstrap-table-zh-CN.min.js"></script>
</head>

<body>
	<div class="white_content"></div>

	<!--开方-->
	<div class="container-2" id="kf">
		<!-- 选择药方后弹出遮罩层 -->
		<div id="xyt_kf_yfmb_popLayer"></div>
		<!-- 选择药方后弹出遮罩层结束 -->
		<a href="javascript:void(0)" onclick="parent.ymPrompt.doHandler('close');">
			<button type="button" class="go-back">&lt;返回视频</button></a>
		<div class="tip">
			<p>
				<span><b>就诊编号：</b><span id="kf-patientno">IK009821</span></span>
				<span><b>姓名：</b><span id="kf-patientname">张三</span></span>
				<span><b>性别：</b><span id="kf-patientsex">男</span></span>
				<span><b>年龄：</b><span id="kf-patientage">56</span></span>
				<span><b>病史：</b><span id="kf-patienthistory"></span></span>
			</p>
			<p><b>诊断：</b>——————————————————————————————————————————————————</p>
			<p><b>处理意见：</b>——————————————————————————————————————————————————</p>
		</div>

		<div class="notice">
			<div class="tab-hd">
				<ul class="tab-nav">
					<!-- <li>草药</li> -->
					<li>颗粒剂</li>
				</ul>

			</div>
			<div class="tab-bd">
				<div class="tab-pal clearfix">
					<div class="xyt_kf_nav">
						<ul class="xyt_kf_zt">
							<li>嘱托：</li>
							<li>共<input id="input-course"></input>付，</li>
							<li>每日<input id="input-rate"></input>次;</li>
							<li>使用：
								<select id="input-state">
									<option value="口服">口服</option>
									<option value="外用">外用</option>
								</select>
							</li>
						</ul>
						<button class="xyt_kf_xzmb" id="xyt_kf_yfmb_popBox_btn">选择模板</button>
						<div id="xyt_kf_yfmb_popBox">
							<div class="xyt_kf_yfmb_close">
								<a href="javascript:void(0)" id="xyt_kf_yfmb_closeBox_btn">
									<li></li>
								</a>
							</div>
							<div class="xyt_kf_yfmb_content">
								<button id="select-prescript-btn">选择药方</button>
								<div class="xyt_kf_yfmb_content_left">
									<ul>
										<li class="xyt_kf_yfmb_content_left_tatil">请选择药方模板</li>
									</ul>
									<ul class="xyt_kf_yfmb_content_left_ul1" id="xyt_kf_yfmb_content_left_ul1_list">

									</ul>
								</div>
								<div class="xyt_kf_yfmb_content_right">
									<ul>
										<li class="xyt_kf_yfmb_content_right_tatil">请选择药品</li>
									</ul>
									<ul class="xyt_kf_yfmb_content_right_ul1">
										<table id="userTable"></table>
									</ul>

								</div>
							</div>
						</div>
					</div>
					<button type="submit" class="sure save-btn">保存药方</button>
					<ul style=" float:left; width: 557px;">
						<li class="lf-tab" id="perscript-table-keli">处方药 <span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 药方名称:</span><input style="width: 120px; height:30px;background-color:bisque" id="perscript-name"></input></li>
						<ul style="border-left: solid 1px #d9d9d9; overflow: hidden; float: left;" id="perscript-list-ul">

						</ul>
					</ul>
					<ul style=" float:right; width: 734px;">
						<!-- <form class="form-inline" role="form">
								<div class="form-group" style="position: absolute; top: -3px; right: 230px;">
								  <input type="text" class="form-control" id="spell-name-keli" placeholder="请输入药品简拼搜索">
								</div>
							  </form> -->
						<table border="" cellspacing="" cellpadding="" width="734" class="rg-tab" id="caoyao-table-keli">
							<tr>
								<th width="30px">序号</th>
								<th width="130px" class="t-i10">药品名称</th>
								<th width="50px" class="t-i10">用量</th>
								<th width="30px">单位</th>
								<th width="180px">备注</th>
								<th width="62px">单价</th>
								<th width="60px">金额</th>
								<th width="80px">操作</th>
							</tr>
							<tr>
								<td></td>
								<td><input class="search-name-input" id="email"></input>
									<ul class="xyt_medical_list"></ul>
								</td>
								<td><input style="width: 50px;"></input></td>
								<td></td>
								<td><input></input></td>
								<td></td>
								<td></td>
								<td></td>
							</tr>

						</table>
					</ul>
				</div>

			</div>
		</div>

	</div>

	</div>
	<script src="../js/main-up.js"></script>
	<script>
		window.baseUrl = localStorage.getItem("baseUrl");
		window.sessionid = localStorage.getItem("sessionid");
		window.selectpescriptid = "";
		var doctorId = localStorage.getItem("doctorId");
		$(function () {

			var $table = $('#userTable');
			var $remove = $('#btn_delete'),
				$add = $('#btn_add'),
				$edit = $('#btn_edit'),
				toolbar = "#userToolbar";

			$table.bootstrapTable({
				contentType: "application/x-www-form-urlencoded",
				method: "post",
				toolbar: toolbar,
				//showRefresh: true,
				//showToggle: true,
				pagination: false,
				//showColumns: true,
				idField: "id",
				sortName: 'createtime',
				sortOrder: 'desc',
				pageSize: 10,
				pageList: [10, 25, 50, 100],
				queryParamsType: "",
				queryParams: function (params) {
					//将查询条件添加到查询参数中，分页或者刷新时将携带这些参数
					params["pageNumber"] = params.pageNumber;
					params["pageSize"] = params.pageSize;
					params["sortOrder"] = params.sortOrder;
					params["sortName"] = params.sortName;
					params["perscriptid"] = window.selectpescriptid;
					return params;
				},
				sidePagination: "server",
				url: window.baseUrl + "/xunyitang/westPrescriptMedical/permedlist?jsessionid=" + window.sessionid,
				columns: [{
						field: "state",
						checkbox: true
					},
					{
						field: "id",
						title: "编号"
					},
					{
						field: "medname",
						title: "药品名称",
						//sortable: true
					},
					{
						field: "medcount",
						title: "用量",
						//	sortable: true
					},
					{
						field: "meddosage",
						title: "单位",
						//	sortable: true
					},
					{
						field: "medprice",
						title: "单价",
						//	sortable: true
					},
					{
						field: "medtotal",
						title: "金额",
						//	sortable: true
					}
				],
				onLoadError: function () { //加载失败时执行
					parent.ymPrompt.doHandler('close');
					window.location.href = "../index.html";
				}
			});
			$table.on('check.bs.table uncheck.bs.table ' +
				'check-all.bs.table uncheck-all.bs.table',
				function () {
					initBtnStatus();
				});
			//数据已添加到table中，onPostBody
			$table.on('post-body.bs.table', function (data) {
				initBtnStatus();
				//为按钮绑定事件
			});

			//数据加载完成事件，onLoadSuccess
			$table.on('load-success.bs.table', function () {
				initBtnStatus();
			});

			function initBtnStatus() {
				var len = $table.bootstrapTable('getSelections').length;
				$add.prop('disabled', !len);
				// $edit.prop('disabled', len!=1);
			}

			function getIdSelections() {
				return $.map($table.bootstrapTable('getSelections'), function (row) {
					return row.medid
				});
			}
			function getRowSelections() {
				return $.map($table.bootstrapTable('getSelections'), function (row) {
					return row
				});
			}
			//刷新并跳转到第一页页面
			function refresh() {
				//刷新并跳转到第一页
				//$table.bootstrapTable('selectPage', 1);
				$table.bootstrapTable('refresh');
			};

			$(".save-btn").click(function () {
				var tempname=$("#perscript-name").val();
				if(tempname==null || tempname.length==0){
					alert("请输入药方名称.");
					return;
				}
				var course = $("#input-course").val();
				if (course==null || course.length==0){
					alert("请输入付数.");
					return;
				}
				var rate=$("#input-rate").val();
				if (rate==null || rate.length==0){
					alert("请输入每日次数.");
					return;
				}

				var numArr = []; // 定义一个空数组
				var txt = $('#perscript-list-ul').find(':text'); // 获取所有文本框
				for (var i = 0; i < txt.length; i++) {
					numArr.push(txt.eq(i).val()); // 将文本框的值添加到数组中
				}
				var select = $('#input-state option:selected') .val();
				var patientid = localStorage.getItem("sessionpatientid");
				var doctorid = localStorage.getItem("doctorId");
				$.ajax({
					type: "POST",
					url: window.baseUrl +
						"/xunyitang/westPrescriptMedical/savepermedlist?jsessionid=" + window
						.sessionid,
					contentType: "application/x-www-form-urlencoded",
					data: {
						caoyaolist: numArr.join(","),
						perscriptname:tempname,
						patientid: patientid,
						doctorid: doctorid,
						course:course,
						rate:rate,
						state:select
					},
					dataType: "json",
					async: false,
					beforeSend: function (request) {
						request.setRequestHeader("Token", localStorage.getItem("Authorization"));
					},
					success: function (data) {

					},
					error: function (data) {
						parent.ymPrompt.doHandler('close');
						window.location.href = "../index.html";
					}
				});

				alert("保存成功");
			});

//选择药方模板
			$("#select-prescript-btn").click(function () {
				var lists = getRowSelections();
				if (null != lists && lists.length > 0) {
					
					$("#xyt_kf_yfmb_popBox").hide();
					$("#xyt_kf_yfmb_popLayer").hide();
					
					for (let index = 0; index < lists.length; index++) {
						const element = lists[index];
						var tempid= element.medid;
                        var tempdosage=element.meddosage;
						var tempprice= element.medprice;
						var tempname=element.medname;
						var tempcount =element.medcount;
						var temptotal=element.medtotal;
						var tempremark=element.remark;
						$("#email").parent().parent().before('<tr><td class="span-perscrpt-id" style="display:none">' +tempid + '</td><td></td><td class="perscript-name-td">'+tempname+'</td><td class="count-td"><input style="width: 50px;" class="count-input" value="'+tempcount+'"></input></td><td>'+tempdosage+'</td><td><input class="remark-input" value="'+tempremark+'"></input></td><td class="price-td">'+tempprice+'</td><td class="total-price-td">'+temptotal+'</td><td><button type="button" class="add-2 add-class-'+tempid+'"></button><button  type="button" class="add-3 remove-class-'+tempid+'"></button></td></tr>');

						$("#email").val("");

						$(".count-input").on(" input propertychange", function () {
							var keyword = $(".count-input").val();
							if(keyword!=null && keyword.length>0 && checkRate(keyword)==true){
								var price=$(this).parent().parent().children(".price-td").text();
								$(this).parent().parent().children(".total-price-td").text((keyword*price).toFixed(2));
							}else{
								$(this).parent().parent().children(".total-price-td").text("");
								}
							});
						$(".add-class-"+tempid).click(function(){
							var tempid=$(this).parent().parent().children(".span-perscrpt-id").text(); 
							var tempname=$(this).parent().parent().children(".perscript-name-td").text(); 
							var tempcount=$(this).parent().parent().children(".count-td").children(".count-input").val(); 
							$("#perscript-list-ul").append('<li class="lf-tab2"><span style="width: 100px;">'+tempname+'</span><span style="width: 10px; float: right;">g</span><span style="width: 20px; float: right; text-align: right;"><input value="'+tempcount+'"  style="width: 20px; height:30px;" readonly></input></span> <input value="'+tempid+'"  style="display:none;"></input></li>');
						});
						$(".remove-class-"+tempid).click(function(){
							var tempid=$(this).parent().parent().children(".span-perscrpt-id").text();
							$("#perscript-list-ul li input[value='"+tempid+"']").parent().remove();
							$(this).parent().parent().empty();
						});
					}
								
				} else {
					alert("请选择药方");
				}

			});
			/*药方弹出框-点击弹出按钮*/
			$("#xyt_kf_yfmb_popBox_btn").click(function () {
				$("#xyt_kf_yfmb_popBox").show();
				$("#xyt_kf_yfmb_popLayer").show();

				var doctorid = localStorage.getItem("doctorId");
				$.ajax({
					type: "POST",
					url: window.baseUrl +
						"/xunyitang/westPrescript/prescriptlist?jsessionid=" + window
						.sessionid,
					contentType: "application/x-www-form-urlencoded",
					data: {
						doctorId: doctorid
					},
					dataType: "json",
					async: false,
					beforeSend: function (request) {
						request.setRequestHeader("Token", localStorage.getItem("Authorization"));
					},
					success: function (data) {
						if (null != data && null != data.rows && data.rows.length > 0) {
							$("#xyt_kf_yfmb_content_left_ul1_list").empty();
							for (var i = 0; i < data.rows.length; i++) {
								var prescript = data.rows[i];
								$("#xyt_kf_yfmb_content_left_ul1_list").append('<li><a class="show-prescript-a" data-id="' +
									prescript.id + '">' + prescript.name + '</a></li>');
							}
							window.selectpescriptid = data.rows[0].id;
							refresh();
							$(".show-prescript-a").click(function () {
								window.selectpescriptid = $(this).data().id;
								refresh();
							});
						}
					},
					error: function (data) {
						parent.ymPrompt.doHandler('close');
						window.location.href = "../index.html";
					}
				});

			});
			/*药方弹出框-点击关闭按钮*/
			$("#xyt_kf_yfmb_closeBox_btn").click(function () {
				$("#xyt_kf_yfmb_popBox").hide();
				$("#xyt_kf_yfmb_popLayer").hide();
			});

		})
	</script>
</body>

</html>